﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Metadata.Edm;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.ApplicationServices;
using System.Web.Mvc;
using System.Web.Routing;
using Newtonsoft.Json;
using LiuYan.Entity;
using LiuYan.Service;

namespace LiuYan.Web.Areas.Admin.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Request.Cookies["user"] == null)
            {
                Session["user"] = null;
            }
            else
            {
                var str = Request.Cookies["user"].Value;
                Session["user"] = JsonConvert.DeserializeObject<TB_Admin>(str);
            }

            if (filterContext.HttpContext.Session["user"] == null)
            {
                filterContext.Result =
                    new RedirectToRouteResult(
                        new RouteValueDictionary(new {Controller = "Login", Action = "Index", id = "1"})); //这里是跳转
            }
            else
            {
               
            }
          
        }

        /// <summary>
        /// 获取登录用户的信息
        /// </summary>
        /// <returns></returns>
        public TB_Admin UserInfo()
        {
            var str = Request.Cookies["user"].Value;
            if (Request.Cookies["user"] == null)
            {
                return new TB_Admin();
            }
            var entity = JsonConvert.DeserializeObject<TB_Admin>(str);
            entity.Name = Server.UrlDecode(entity.Name);
            return entity;
        }

    }
}
